/*
 * Copyright 2013 The Scripps Research Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.scripps.fl.entrez.internal;

import java.util.ArrayList;
import java.util.Collection;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ESearchHandler extends DefaultHandler {
	private StringBuffer buf;
	private int count;
	private int depth = 0;
	private Collection<Long> ids;
	private int limit;
	private String queryKey;
	private int retMax;
	private int retStart;
	private String webEnv;

	public ESearchHandler(Collection<Long> ids) {
		this(ids, -1);
	}

	public ESearchHandler(Collection<Long> ids, int limit) {
		this.ids = ids;
		this.limit = limit;
	}

	public void characters(char ch[], int start, int length) throws SAXException {
		buf.append(ch, start, length);
	}

	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (qName.equalsIgnoreCase("Id")) {
			Long id = Long.parseLong(buf.toString());
			ids.add(id);
			if (limit > 0 && ids.size() >= limit)
				throw new SAXException("Limit reached");
		} else if (qName.equalsIgnoreCase("WebEnv"))
			webEnv = buf.toString();
		else if (qName.equalsIgnoreCase("QueryKey"))
			queryKey = buf.toString();
		else if (qName.equalsIgnoreCase("RetMax"))
			retMax = Integer.parseInt(buf.toString());
		else if (qName.equalsIgnoreCase("RetStart"))
			retStart = Integer.parseInt(buf.toString());
		else if (depth == 2 && qName.equalsIgnoreCase("Count")) {
			count = Integer.parseInt(buf.toString());
			if (ids instanceof ArrayList)
				((ArrayList<Long>) ids).ensureCapacity(ids.size() + count);
		}
		depth--;
	}

	public int getCount() {
		return count;
	}

	public String getQueryKey() {
		return queryKey;
	}

	public int getRetMax() {
		return retMax;
	}

	public int getRetStart() {
		return retStart;
	}

	public String getWebEnv() {
		return webEnv;
	}

	public boolean isLimitReached() {
		return ids.size() >= limit;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public void setQueryKey(String queryKey) {
		this.queryKey = queryKey;
	}

	public void setRetMax(int retMax) {
		this.retMax = retMax;
	}

	public void setRetStart(int retStart) {
		this.retStart = retStart;
	}

	public void setWebEnv(String webEnv) {
		this.webEnv = webEnv;
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		depth++;
		buf = new StringBuffer();
	}
}